#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\Resources\Icons\Install-Green.ico
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Get-App Client", 291, 111, 192, 114)
$lblAppName = GUICtrlCreateLabel("Application Name:", 8, 8, 90, 17)
$ibxAppName = GUICtrlCreateInput("", 104, 8, 121, 21)
$lblVer = GUICtrlCreateLabel("Version:", 56, 40, 42, 17)
$ibxVer = GUICtrlCreateInput("", 104, 40, 121, 21)
$cbxLatest = GUICtrlCreateCheckbox("Latest", 232, 40, 49, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$btnInstall = GUICtrlCreateButton("Install", 112, 80, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$server = "http://localhost"
$dataPort = "277"
$filePort = "438"
$latest = True
testCheck()
While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $btnInstall
			$appInfoCSV = GetAppInfo()
			If $appInfoCSV == "" Then
				MsgBox(0,"","No such application")
			Else
				InstallFile()
			EndIf
			Exit
		Case $cbxLatest
			testCheck()
		Case $GUI_EVENT_CLOSE
			Exit

	EndSwitch
WEnd

Func testCheck()
	If GUICtrlRead($cbxLatest) == 1 Then
		GUICtrlSetState($lblVer, $GUI_DISABLE)
		GUICtrlSetState($ibxVer, $GUI_DISABLE)
		$latest = True
	Else
		GUICtrlSetState($lblVer, $GUI_ENABLE)
		GUICtrlSetState($ibxVer, $GUI_ENABLE)
		$latest = False
	EndIf
EndFunc

Func GetAppInfo()
	If $latest == True Then
		$uri = $server & ":" & $dataPort & "/" & @ComputerName & "/" & GUICtrlRead($ibxAppName)
	Else
		$uri = $server & ":" & $dataPort & "/" & @ComputerName & "/" & GUICtrlRead($ibxAppName) & "/" & GUICtrlRead($ibxVer)
	EndIf
	Local $sData = InetRead($uri)
	Local $nBytesRead = @extended
	Return BinaryToString($sData)
EndFunc

Func InstallFile()
	Local $appInfo = StringSplit($appInfoCSV,",")
	InetGet($server & ":" & $filePort & "/" & $appInfo[2],@ScriptDir & "\" & $appInfo[1])
	RunWait($appInfo[1] & " " & $appInfo[3])
EndFunc